package com.whfc.fuum.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whfc.common.enums.DelFlag;
import com.whfc.common.enums.EnableState;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.PageData;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.PageUtil;
import com.whfc.fuum.dao.SysAlgorithmTypeMapper;
import com.whfc.fuum.dto.SysAlgorithmTypeDTO;
import com.whfc.fuum.entity.SysAlgorithmType;
import com.whfc.fuum.param.SysAlgorithmTypeAddParam;
import com.whfc.fuum.param.SysAlgorithmTypeEditParam;
import com.whfc.fuum.service.SysAlgorithmService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;


/**
 * @Description: 系统配置算法服务
 * @Author: likang
 * @Date: 2020-09-22 12:01
 */
@DubboService(interfaceClass = SysAlgorithmService.class, version = "1.0.0")
public class SysAlgorithmServiceImpl implements SysAlgorithmService {

    @Autowired
    private SysAlgorithmTypeMapper sysAlgorithmTypeMapper;


    @Override
    public void addAlgorithmType(SysAlgorithmTypeAddParam param) throws BizException {
        String name = param.getName();
        Integer type = param.getType();
        //判断据库中编码和名称重复
        SysAlgorithmType algorithmN = sysAlgorithmTypeMapper.selectAlgorithmByName(name);
        if (algorithmN != null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "算法类型名称已存在");
        }
        SysAlgorithmType algorithmT = sysAlgorithmTypeMapper.selectAlgorithmByType(type);
        if (algorithmT != null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "算法类型代码已存在");
        }

        //数据库中不存在
        SysAlgorithmType algorithmType = new SysAlgorithmType();
        algorithmType.setName(name);
        algorithmType.setType(type);
        algorithmType.setIconUrl(param.getIconUrl());
        algorithmType.setState(EnableState.ENABLED.getValue());
        algorithmType.setDelFlag(DelFlag.UNDELETE.getValue());
        Date date = new Date();
        algorithmType.setCreateTime(date);
        algorithmType.setUpdateTime(date);
        sysAlgorithmTypeMapper.insert(algorithmType);

    }


    @Override
    public void editAlgorithmType(SysAlgorithmTypeEditParam param) throws BizException {

        //判断据库中编码和名称重复
        SysAlgorithmType algorithmN = sysAlgorithmTypeMapper.selectAlgorithmByNameExcludeId(param.getId(), param.getName());
        if (algorithmN != null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "算法类型名称已存在");
        }
        SysAlgorithmType algorithmT = sysAlgorithmTypeMapper.selectAlgorithmByTypeExcludeId(param.getId(), param.getType());
        if (algorithmT != null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "算法类型代码已存在");
        }

        SysAlgorithmType algorithmType = new SysAlgorithmType();
        BeanUtils.copyProperties(param, algorithmType);
        algorithmType.setUpdateTime(new Date());
        sysAlgorithmTypeMapper.updateAlgorithmByParam(algorithmType);
    }


    @Override
    public void modifyAlgorithmState(Integer id, Integer state) throws BizException {
        if (state != null && state.equals(EnableState.ENABLED.getValue())) { //处于启用状态
            sysAlgorithmTypeMapper.disableAlgorithmType(id);
        } else {
            sysAlgorithmTypeMapper.enableAlgorithmType(id);
        }
    }


    @Override
    public void deleteAlgorithmType(Integer id) throws BizException {
        sysAlgorithmTypeMapper.logicDeleteById(id);
    }


    @Override
    public PageData<SysAlgorithmTypeDTO> getAlgorithmTypeList(String keyword, Integer state, Integer pageNum, Integer pageSize) throws BizException {
        PageHelper.startPage(pageNum, pageSize);
        List<SysAlgorithmTypeDTO> list = sysAlgorithmTypeMapper.selectAlgorithmTypeList(keyword, state);
        PageHelper.clearPage();
        return PageUtil.pageData(PageInfo.of(list));
    }

    @Override
    public List<SysAlgorithmTypeDTO> getAlgorithmTypeList(String keyword, Integer state) throws BizException {
        return sysAlgorithmTypeMapper.selectAlgorithmTypeList(keyword, state);
    }

    @Override
    public List<SysAlgorithmTypeDTO> getAlgorithmInTypes(List<Integer> types) throws BizException {
        return sysAlgorithmTypeMapper.selectAlgorithmInTypes(types);
    }

    @Override
    public SysAlgorithmTypeDTO getAlgorithmInType(Integer type) throws BizException {
        return sysAlgorithmTypeMapper.selectAlgorithmInType(type);
    }

    @Override
    public List<SysAlgorithmTypeDTO> getAlgorithmNotInTypes(List<Integer> types) throws BizException {
        return sysAlgorithmTypeMapper.selectAlgorithmNotInTypes(types);
    }
}
